
package me.zhengjie.service.impl;

import me.zhengjie.domain.AppDocCate;
import me.zhengjie.base.TreeNodes;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.AppDocCateRepository;
import me.zhengjie.service.AppDocCateService;
import me.zhengjie.service.dto.AppDocCateDto;
import me.zhengjie.service.dto.AppDocCateQueryCriteria;
import me.zhengjie.service.mapstruct.AppDocCateMapper;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author IQW
* @date 2022-07-18
**/
@Service
@RequiredArgsConstructor
public class AppDocCateServiceImpl implements AppDocCateService {

    private final AppDocCateRepository appDocCateRepository;
    private final AppDocCateMapper appDocCateMapper;

    @Override
    public Map<String,Object> queryAll(AppDocCateQueryCriteria criteria, Pageable pageable){
        Page<AppDocCate> page = appDocCateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(appDocCateMapper::toDto));
    }

    @Override
    public List<AppDocCateDto> queryAll(AppDocCateQueryCriteria criteria){
        return appDocCateMapper.toDto(appDocCateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public List<TreeNodes> queryAll2(AppDocCateQueryCriteria criteria){

        Sort sort = Sort.by(Sort.Direction.ASC, "ordid");
        criteria.setStatus(1);
        List<AppDocCate> list = appDocCateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),sort);
        List<TreeNodes> rets = new LinkedList<>();
        for (AppDocCate cate : list){
             if(cate.getFid() == 0) {
                 TreeNodes tmp = new TreeNodes(cate.getCateId(),1,cate.getTitle());
                 rets.add(tmp);
             }
        }
        for (AppDocCate cate : list){
            for (TreeNodes cate2 : rets) {
                if (cate.getFid().equals(cate2.getId())) {
                    TreeNodes tmp = new TreeNodes(cate.getCateId(), 2, cate.getTitle());
                    if(null == cate2.getChildren() || cate2.getChildren().size() == 0){
                        List<TreeNodes> tmps = new LinkedList<>();
                        tmps.add(tmp);
                        cate2.setChildren(tmps);
                    }else{
                        List<TreeNodes> tmpNew = cate2.getChildren();
                        tmpNew.add(tmp);
                        cate2.setChildren(tmpNew);
                    }
                }
            }
        }
         return rets;
    }

    @Override
    @Transactional
    public AppDocCateDto findById(Integer cateId) {
        AppDocCate appDocCate = appDocCateRepository.findById(cateId).orElseGet(AppDocCate::new);
        ValidationUtil.isNull(appDocCate.getCateId(),"AppDocCate","cateId",cateId);
        return appDocCateMapper.toDto(appDocCate);
    }

    @Override
    public List<AppDocCate> findByFid(Integer fid) {
        return appDocCateRepository.findByFid(fid);
    }

    @Override
    public List<Integer> getChildrenIds(List<AppDocCate> lists) {
        List<Integer> list = new ArrayList<>();
        lists.forEach(dept -> {
                    if (dept!=null && dept.getStatus()==1) {
                        List<AppDocCate> depts = appDocCateRepository.findByFid(dept.getCateId());
                        if (depts.size() != 0) {
                            list.addAll(getChildrenIds(depts));
                        }
                        list.add(dept.getCateId());
                    }
                }
        );
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppDocCateDto create(AppDocCate resources) {
        return appDocCateMapper.toDto(appDocCateRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AppDocCate resources) {
        AppDocCate appDocCate = appDocCateRepository.findById(resources.getCateId()).orElseGet(AppDocCate::new);
        ValidationUtil.isNull( appDocCate.getCateId(),"AppDocCate","id",resources.getCateId());
        appDocCate.copy(resources);
        appDocCateRepository.save(appDocCate);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer cateId : ids) {
            appDocCateRepository.deleteById(cateId);
        }
    }

    @Override
    public void download(List<AppDocCateDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AppDocCateDto appDocCate : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("责任区ID", appDocCate.getAreaId());
            map.put("父级ID", appDocCate.getFid());
            map.put("名称", appDocCate.getTitle());
            map.put("1政策文件，2知识文档", appDocCate.getType());
            map.put("图标", appDocCate.getIcon());
            map.put("排序，越小越靠前", appDocCate.getOrdid());
            map.put("状态", appDocCate.getStatus());
            map.put(" createTime",  appDocCate.getCreateTime());
            map.put(" updateTime",  appDocCate.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}